package com.android.webttest.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.android.webttest.model.tableModel.SearchHistoryRecord;
import com.android.webttest.R;

import java.util.List;

public class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.SearchHistoryHolder> {

    private Context mcontext;
    private SearchHistoryAdapter.onItemClickListener mlistener;
    private List<SearchHistoryRecord> mlist;

    public SearchHistoryAdapter(Context context, List<SearchHistoryRecord> list, SearchHistoryAdapter.onItemClickListener listener) {
        this.mcontext = context;
        this.mlistener = listener;
        this.mlist = list;
    }

    @NonNull
    @Override
    public SearchHistoryHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SearchHistoryAdapter.SearchHistoryHolder(LayoutInflater.from(mcontext).inflate(R.layout.search_history_item, parent, false));
//        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_history_item, parent, false);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchHistoryHolder holder, int position) {
        if (mlist != null && mlist.size() > 0) {
            holder.textView.setText(mlist.get(position).getQuery());
        }

        holder.itemView.setOnClickListener(v -> {
            int adapterPosition = holder.getAdapterPosition();
            if (adapterPosition != RecyclerView.NO_POSITION) {
                mlistener.onClick(adapterPosition);
                Toast.makeText(mcontext, "点击了第" + adapterPosition + "个item"+mlist.get(adapterPosition).getQuery(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    public class SearchHistoryHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public SearchHistoryHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.search_h_d);
        }
    }

    public interface onItemClickListener {
        void onClick(int pos);
    }
}

